package com.dream.controller;

import com.dream.model.User;
import com.dream.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class LoginController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/indexPage")
    public String indexPage() {
        return "index";

    }

    @RequestMapping(value = "/user/login")
    public String login() {
        return "login/login";
    }

    @RequestMapping(value = "/user/loginCheck")
    public String loginCheck(HttpServletRequest request, HttpServletResponse response, User user) {
        if (user == null) {
            try {
                response.sendRedirect("/user/login");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        boolean result = userService.usersLogin(user);
        if (result) {
            return "login/success";
        }
        return "login/login";
    }

    @RequestMapping(value = "/user/register")
    public String register() {
        return "login/register";
    }

    @RequestMapping(value = "/user/registerCheck")
    public String registerCheck(User user) {
        boolean result = userService.usersRegister(user);
        if (result) {
            return "login/success";
        }
        return "login/register";
    }

}

